/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package doa;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import models.Message;
import models.Twitterer;
import org.hibernate.Query;
import org.hibernate.Transaction;

/**
 *
 * @author Misja
 */
public class TwittererDAO {

    public static List<Twitterer> getTwitterers() {
        List<Twitterer> twitterers = new ArrayList<Twitterer>();
        try {
            HibernateSessionController.Instance().initSession();
            Transaction tx = HibernateSessionController.Instance().getHibernateSession().beginTransaction();
            twitterers = HibernateSessionController.Instance().getHibernateSession().createQuery("from Twitterer").list();
            tx.commit();
        } catch (Exception e) {
        }
        return twitterers;
    }

    public static boolean saveTwitterer(Twitterer twitterer) {
        boolean result = false;
        try {
            HibernateSessionController.Instance().initSession();
            Transaction tx = HibernateSessionController.Instance().getHibernateSession().beginTransaction();
            HibernateSessionController.Instance().getHibernateSession().save(twitterer);
            tx.commit();
            result = true;
        } catch (Exception e) {
        }
        return result;
    }

    public static boolean updateTwitterer(Twitterer twitterer) {
        boolean result = false;
        try {
            HibernateSessionController.Instance().initSession();
            Transaction tx = HibernateSessionController.Instance().getHibernateSession().beginTransaction();
            HibernateSessionController.Instance().getHibernateSession().flush();
            HibernateSessionController.Instance().getHibernateSession().merge(twitterer);
            tx.commit();
            result = true;
        } catch (Exception e) {
        }
        return result;
    }

    public static boolean deleteTwitterer(Twitterer twitterer) {
        boolean result = false;
        boolean deleted = true;
        List<Message> messages = MessageDAO.getMessagesByOwner(twitterer);
        Iterator<Message> it = messages.iterator();
        while (it.hasNext()) {
            if (!MessageDAO.deleteMessage(it.next())) {
                deleted = false;
            }
        }
        if (deleted) {
            try {


                HibernateSessionController.Instance().initSession();
                Transaction tx = HibernateSessionController.Instance().getHibernateSession().beginTransaction();
                HibernateSessionController.Instance().getHibernateSession().flush();
                HibernateSessionController.Instance().getHibernateSession().delete(twitterer);
                tx.commit();
                result = true;
            } catch (Exception e) {
            }
        }
        return result;
    }
}
